package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.RoleVo;
	import engine.flag.ResourcesPath;
	import engine.logic.event.LogicEvent;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.tip.TipsMainLogic;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.button.NxButton;

	public class TopOtherPlayerLogic
	{
		private static var _self:TopOtherPlayerLogic;
		public static function getInstance():TopOtherPlayerLogic
		{
			if(_self==null)
			{
				_self=new TopOtherPlayerLogic();
			}
			return _self;
		}
		private const NAME:String="nameTxt";
		private const LEVEL:String="rankTxt";
		private const FACE:String="face";
		private var _bindLevelFlag:int=-1;
		private var _ui:TopOtherPlayerUI;
		
		private var _closeModel:int;
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		private var _nx:NxClip=new NxClip;
		
		private var _roleName:String;
		private var _rank:int;
		private var _gang:String;
		private var _fightPower:int;
		private var _peer:int;
		private var _priority:int
		
		private var _text:TextField=new TextField;
	
		private var _stateArr:Vector.<BtnState>;
		private var _otherLv:int;
		private var _needArr:Array=[];
		public function  Init():void
		{
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_ROLE_LOOK_PLAYER_TIPS),init);
//			UIString_1000097	*查看资料*
//				UIString_1000098	*加为好友*
//					UIString_1000099	*发送私聊*
//			_ui.btn1.label=StaticIni.getSwfLangStr("UIString_1000097");
//			_ui.btn2.label=StaticIni.getSwfLangStr("UIString_1000098");
//			_ui.btn3.label=StaticIni.getSwfLangStr("UIString_1000099");
//			_ui.btn4.label=StaticIni.getSwfLangStr("UIString_1000261");
			saveBtnNeedLv();
			_ui.label1.mouseEnabled=_ui.label2.mouseEnabled=_ui.label3.mouseEnabled=_ui.label4.mouseEnabled=false;
		}
		
		public function	BeforeShow():void
		{
			if(!SceneManager._itemIconLoad)
			{
				_model.addEventListener(LogicEvent.ITEMICONLOADED,loaded);
				function loaded():void
				{
					face=curPlayerUrl
					_model.removeEventListener(LogicEvent.ITEMICONLOADED,loaded)
				}
			}
			addAllEventListener()
		}
		public function	AfterHide():void
		{
			removeAllEventListener()
	
		}
		public function addAllEventListener():void
		{
			_nx.addEventListener(MouseEvent.ROLL_OVER,faceRoll);
			StageInfo.uiSpace.addEventListener(MouseEvent.CLICK,clickHide);
			_nx.addEventListener(MouseEvent.ROLL_OUT,_tip.hide);
			_ui.addEventListener(MouseEvent.CLICK,mcClickHandle);

		}
		private function saveBtnNeedLv():void
		{
			var NeedArr:Array=[];
			var obj:Object=StaticIni.getIniObj("TopOther_Need");
			for (var i:int = 1; i <= 4; i++) 
			{
				if(obj.hasOwnProperty("lvl"+i))
				{
					NeedArr[i-1]=obj["lvl"+i];
					if(int(NeedArr[i-1]))
					{
						changeState(i,int(NeedArr[i-1]));
					}
				}
			}
			_needArr=NeedArr;
		}
		public function  myLevel():void
		{
			var needArr:Array=_needArr;
			var lv:int=ModelLocator.getInstance().scene.Role.getPlayerLevel();
			_stateArr=new Vector.<BtnState>();
			for (var i:int = 0; i < 4; i++) 
			{
				var self:int;
				var other:int;
				if(_otherLv<int(needArr[i]))
				{
					other=0;
				}
				else
				{
					other=1;
				}
				if(lv<int(needArr[i]))
				{
					self=0;
				}
				else
				{
					self=1;
				}
				_stateArr.push(new BtnState(self,other));
			}
			
		}
		private function changeState(index:int,state:int):void
		{
			_ui["label"+index].visible=state>0;
			_ui["btn"+index].mouseEnabled	=	_ui["btn"+index].mouseChildren=state>0;
//			_ui["btn"+index].enabled=state>0;
		}
		private function mcClickHandle(e:Event):void
		{
			var needArr:Array=_needArr;
			myLevel();
			var btn:NxButton=e.target as NxButton;
			switch(btn)
			{
				case _ui.btn1://查看
				{

					if(_stateArr[0].selfState==1)
					{
						ActorPropertyLogic.getInstance().lookOver(roleName);
					}
					else 
					{
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStrVar("UIString_1000396",[needArr[0]]));
					}
				
					break;
				}
				case _ui.btn2://加为好友
				{
					if(_stateArr[1].otherState==1 && _stateArr[1].selfState==1)
					{
						FriendLogic.getInstance().sendAddFriend(roleName);
					}
					else if(_stateArr[1].selfState==0)
					{
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStrVar("UIString_1000396",[needArr[1]]));
					}
					else if(_stateArr[1].otherState==0)
					{
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStrVar("UIString_1000397",[needArr[1]]));
					}
				
				
					break;
				}
				case _ui.btn3://发送私聊
				{
					if(_stateArr[2].otherState==1 && _stateArr[2].selfState==1)
					{
						WhisperLogic.getInstance().whisperSart(roleName);
					}
					else if(_stateArr[2].selfState==0)
					{
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStrVar("UIString_1000396",[needArr[2]]));
					}
					else if(_stateArr[2].otherState==0)
					{
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStrVar("UIString_1000397",[needArr[2]]));
					}
					
					
					break;
				}
				case _ui.btn4://推演运功
				{
					if(_stateArr[3].otherState==1 && _stateArr[3].selfState==1)
					{
						//请求推演运功
						SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_COUPLE_AFK_REQUEST,new WString(roleName));
					}
					else if(_stateArr[3].selfState==0)
					{
						//自己条件不满足
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStrVar("UIString_1000396",[needArr[3]]));
					}
					else if(_stateArr[3].otherState==0)
					{
						//对方不满足
						NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStrVar("UIString_1000397",[needArr[3]]));
					}
					break;
				}
				default:
				{
					break;
				}
			}
		}
		private function faceRoll(e:MouseEvent):void
		{
			_tip.showMain(StaticIni.getSwfLangStrVar("UIString_1000080",[_rank,_gang,_fightPower,_peer,_priority]));
				//排名&nbsp;{@0:value}<br/>帮派&nbsp;{@1:帮派名}<br/>战力&nbsp;{@2:fightpower}<br/>名望&nbsp;{@3:value}<br/>先攻
		}
		public function removeAllEventListener():void
		{
			_ui.removeEventListener(MouseEvent.CLICK,mcClickHandle);
			_nx.removeEventListener(MouseEvent.ROLL_OVER,faceRoll);
			StageInfo.uiSpace.removeEventListener(MouseEvent.CLICK,clickHide);
			_nx.removeEventListener(MouseEvent.ROLL_OUT,_tip.hide);
		}
		private function clickHide(e:Event):void
		{
			var dp:DisplayObject=e.target as DisplayObject;
			if(!_ui.contains(dp))
				hide();
		}
		private var curPlayerUrl:int;
		public function init(e:CustomMsgEvent):void
		{
			_ui.y	=	TopPlayerUI.getInstance().y;
			_ui.x	=	TopPlayerUI.getInstance().width+TopPlayerUI.getInstance().x;
			var arr:Array=e.pList;
//			trace("TopOtherPlayerLogic.init(e)",e.pList);
			roleName=arr[0];
			level=_otherLv=arr[2];
			face=curPlayerUrl=arr[1]
				//排名、帮派、战力、名望、先攻
			_rank=arr[3];
			_gang=arr[4]?arr[4]:StaticIni.getSwfLangStr( "Common_Null" );
			_fightPower=arr[5];
			_peer=arr[6];
			_priority=arr[7];
			_ui.show(TopPlayerUI.getInstance());
			var p:Point=new Point(TopPlayerUI.getInstance().topPlayerBg.x,TopPlayerUI.getInstance().topPlayerBg.y)
			_ui.x=p.x+300;
			_ui.y=p.y;
			StageInfo.stage.dispatchEvent(new Event(Event.RESIZE));
		}
		public function get roleName():String
		{
			return _roleName;
		}
		public function set roleName(name:String):void 
		{
			_roleName=name
			_ui.play_mc[NAME].htmlText=name;
		}
		public function set level(value:int):void 
		{
			_ui.play_mc[LEVEL].htmlText=value+""
		}
		public function set face(url:int):void 
		{
			_nx.source=ResourcesPath.getHeadPic( url, _closeModel );
			if(!_nx.parent)
			{
				_ui.play_mc[FACE].addChild(_nx);
			}
		}
		public function get ui():TopOtherPlayerUI
		{
			if(_ui==null)
			{
				_ui	=	new TopOtherPlayerUI;
			}
			return _ui;
		}
		public function callForShow(name:String,closeModel:int):void
		{
			_text.htmlText=name;
			_closeModel=closeModel;
			_closeModel=(_closeModel ==0)?1:_closeModel;
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LOOK_PLAYER_TIPS,new WString(_text.text));
			
		}
		/**
		 *隐藏显示 
		 * @param args
		 * 
		 */		
		public function hide(...args):void
		{
			_ui.hide();
		}
	}
}
class BtnState
{
	public var selfState:int;
	public var otherState:int;
	public function BtnState(selfState:int,otherState:int):void
	{
		this.selfState=selfState;
		this.otherState=otherState;
	}
}
